import Vue from 'vue';
import App from './App';
import './assets/icon/iconfont.css';
import VueTouchRipple from 'vue-touch-ripple';
import 'vue-touch-ripple/dist/vue-touch-ripple.css';
import Treeselect from '@riophae/vue-treeselect';
import '@riophae/vue-treeselect/dist/vue-treeselect.css';
// 全局的vux
import { AlertPlugin, ToastPlugin, ConfirmPlugin, LoadingPlugin, TransferDom } from 'vux';
// 无限加载
import infiniteScroll from 'vue-infinite-scroll';
import ImageViewer from 'image-viewer-gallery';

// 支持IE 浏览器
import 'babel-polyfill';
// 数据存储
import db from './db';
// 路由配置
import router from './router';
// 支持$http
import axios from 'axios';
import filters from './filters';
import util from './util';
import VueQriously from 'vue-qriously';
// const $ = require('jQuery');

Vue.config.devtools = true;
Vue.use(AlertPlugin);
Vue.use(ToastPlugin, {position: 'bottom'});
Vue.use(ConfirmPlugin);
Vue.use(LoadingPlugin);
Vue.use(infiniteScroll);
Vue.directive('transfer-dom', TransferDom);
Vue.use(VueTouchRipple, {
  color: '#fff',
  opacity: 0.3,
  speed: 3,
  transition: 'ease'
});

Vue.prototype.$http = axios;
Vue.config.productionTip = false;
Vue.prototype.$db = db;
Vue.prototype.$util = util;
Vue.prototype.$history = [];
Vue.prototype.$ImageViewer = ImageViewer;
Vue.use(VueQriously);
Vue.component('treeselect', Treeselect);

// 统一请求方法
Vue.prototype.$getAndLoad = function (chartName, apiUrl, callback, ...params) {
  Vue.$vux.loading.show();
  axios(apiUrl).then(function (res) {
    if (res.data && res.data.success) {
      callback(res.data, params);
    } else {
      Vue.$vux.toast.text(`${chartName}获取失败`, 'bottom');
    }
    Vue.$vux.loading.hide();
  }, function () {
    Vue.$vux.toast.text(`${chartName}未响应`, 'bottom');
    Vue.$vux.loading.hide();
  });
};

Vue.prototype.$paramPost = function (apiUrl, params, callback, isLog = true) {
  Vue.$vux.loading.show();
  axios.post(apiUrl, params).then(function (res) {
    Vue.$vux.loading.hide();
    callback && callback(res.data);
  });
};

axios.defaults.withCredentials = true;
axios.interceptors.request.use(config => {
  return config;
}, err => {
  Vue.$vux.loading.hide();
  Vue.$vux.toast.text('请求超时');
  return Promise.resolve(err);
});
axios.interceptors.response.use(data => {
  return data;
}, err => {
  Vue.$vux.loading.hide();
  Vue.$vux.toast.text('服务器异常');
  return Promise.resolve(err);
});
for (let key in filters) {
  Vue.filter(key, filters[key]);
}

router.beforeResolve((to, from, next) => {
  if (to.meta.auth) {
    //
  }
  next();
});

router.fail = function (msg) {
  router.push({name: 'Fail',
  params: {
    msg: msg
  }});
};

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
});
